package com.jinyi.wwdeng.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.jinyi.wwdeng.App
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.DialogScoringBinding
import com.jinyi.wwdeng.databinding.DialogScoringCommentBinding

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 打分评价dialog
 */
class ScoringCommentDialog: ProDialog {
    private lateinit var binding: DialogScoringCommentBinding
    private var mCallback: IDialogCallback? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width = App.getPhoneWidth() - App.dp2px(40f)
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(com.jinyi.pickerview.R.style.picker_view_scale_anim)
        window!!.setGravity(Gravity.CENTER)
    }

    override fun getRootView(): View {
        binding = DialogScoringCommentBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.ivClose) //关闭
        setOnClickListener(R.id.btnNotNow)
        setOnClickListener(R.id.btnRate)
        binding.ratingBar.setIsIndicator(false)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.ivClose,
            R.id.btnNotNow -> {//关闭、取消
                dismiss()
            }
            else -> {//确定
                val count = binding.ratingBar.rating.toInt()
                if (count <= 0){
                    return
                }
                if(mCallback != null){
                    mCallback!!.completeScoringCount(count)
                }
                dismiss()
            }
        }
    }

    interface IDialogCallback {
        //返回评星数量
        fun completeScoringCount(count: Int)
    }
}